package com.snake.springboot.authority.service.impl;

import com.snake.springboot.authority.api.model.AuthRoleModel;
import com.snake.springboot.authority.api.service.AuthRoleService;
import com.snake.springboot.authority.dao.mybatis.entity.AuthRoleDO;
import com.snake.springboot.authority.dao.mybatis.mappers.AuthRoleMapper;
import com.snake.springboot.authority.service.assembler.AuthRoleAssembler;
import org.snake.common.utils.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 类 {@code AuthRoleServiceImpl}  <br> .
 * <p>权限角色服务实现类</p>
 * 创建时间：2024/5/27 14:48
 *
 * @author yzhui
 * @see
 */
@Service
public class AuthRoleServiceImpl implements AuthRoleService {

    private AuthRoleMapper authRoleMapper;

    @Autowired
    public AuthRoleServiceImpl(AuthRoleMapper authRoleMapper) {
        this.authRoleMapper = authRoleMapper;
    }

    @Override
    public AuthRoleModel addRole(AuthRoleModel model) throws Exception {
        AuthRoleDO entity = AuthRoleAssembler.assembleAuthRoleDO(model, true);
        authRoleMapper.insert(entity);
        model.setRoleId(entity.getRoleId());
        return model;
    }

    @Override
    public int modifyRole(AuthRoleModel model) throws Exception {
        AuthRoleDO entity = AuthRoleAssembler.assembleAuthRoleDO(model, false);
        return  authRoleMapper.update(entity);
    }

    @Override
    public int removeRole(String roleId) {
        return authRoleMapper.delete(roleId);
    }

    @Override
    public AuthRoleModel querySingleRole(String roleId) {
        AuthRoleDO entity = authRoleMapper.findById(roleId);
        AuthRoleModel result = AuthRoleAssembler.assembleAuthRoleModel(entity);
        return result;
    }

    @Override
    public List<AuthRoleModel> queryRoleList(AuthRoleModel params) throws Exception {
        AuthRoleDO condition = AuthRoleAssembler.assembleAuthRoleDO(params, false);
        List<AuthRoleDO> list = authRoleMapper.findByCondition(condition);
        List<AuthRoleModel> result = ListUtils.copy(list, AuthRoleModel.class);
        return result;
    }
}
